package com.gxa.community.admin.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class CalendarUtils {

    /**
     * 获取最早时间
     * @param time 传入格式Integer，如：1获取当天 2获取本周 3获取本月
     * @return 返回对应的时间戳,类型为 lang 返回-1传参不正确
     */
    public static long time(Integer time) {
        if (time == 1) {
            //查询当天--当前时间到今天00:00:000毫秒
             return CalendarUtils.today();
        }
        if (time == 2) {
            //查询本周--当前时间到本周一00:00:000毫秒
             return CalendarUtils.week();
        }
        if (time == 3) {
            //查询本月--当前时间到本月1号00:00:000毫秒
            return CalendarUtils.month();
        } else {
            return -1;
        }
    }

    /**
     * 获取当前时间的lang
     * @return 返回一个当前时间 lang类型
     */
    public static long now(){
        Calendar c = Calendar.getInstance();
        return c.getTimeInMillis();
    }

    /**
     * 获取本月的第一天的0时0分0秒0毫秒
     * @return 时间挫
     */
    public static long month() {
        //获取当前时间
        Calendar c = Calendar.getInstance(); //获取当前时间
        c.set(Calendar.DAY_OF_MONTH, 1);  //设置本月第一天
        c.set(Calendar.HOUR_OF_DAY, 0);  //设置小时为0
        c.set(Calendar.MINUTE, 0); //设置分钟为0
        c.set(Calendar.SECOND, 0); //设置秒为0
        c.set(Calendar.MILLISECOND, 0);//设置毫秒为0
        return c.getTimeInMillis();
    }

    /**
     * 获取当个天开始的时间的0时0分0秒0毫秒
     * @return 时间挫
     */
    public static long today() {
        //获取当前时间
        Calendar c = Calendar.getInstance(); //获取当前时间
        c.set(Calendar.HOUR_OF_DAY, 0);  //设置小时为0
        c.set(Calendar.MINUTE, 0); //设置分钟为0
        c.set(Calendar.SECOND, 0); //设置秒为0
        c.set(Calendar.MILLISECOND, 0);//设置毫秒为0
        return c.getTimeInMillis();
    }

    /**
     * 设置一个当天的一个某时的时间戳
     * @param time 某时
     * @return 时间戳
     */
    public static long setToday(Integer time) {
        //获取当前时间
        Calendar c = Calendar.getInstance(); //获取当前时间
        c.set(Calendar.HOUR_OF_DAY, time);  //设置小时为0
        c.set(Calendar.MINUTE, 0); //设置分钟为0
        c.set(Calendar.SECOND, 0); //设置秒为0
        c.set(Calendar.MILLISECOND, 0);//设置毫秒为0
        return c.getTimeInMillis();
    }

    public static long now1() {
        //获取当前时间
        Calendar c = Calendar.getInstance(); //获取当前时间
        c.set(Calendar.HOUR_OF_DAY, c.get(Calendar.HOUR_OF_DAY)-1);  //设置小时为0
        return c.getTimeInMillis();
    }

    /**
     * 获取本周的最后一天
     * @return 时间挫
     */
    public static long week() {
        Calendar c = Calendar.getInstance();
        int i = c.get(Calendar.DAY_OF_WEEK);//获取今天是本周的第几天
        if (i == 1) {   //如果是周天就如果是就减一天
            c.add(Calendar.DAY_OF_WEEK, -1);
        }
        c.set(c.get(Calendar.YEAR), c.get(Calendar.MONDAY), c.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return c.getTimeInMillis();
    }

    /**
     * 获取当天最后时间
     * @return 时间挫
     */
    public static long todaytail() {
        //获取当前时间
        Calendar c = Calendar.getInstance(); //获取当前时间
        c.set(Calendar.HOUR_OF_DAY, 23);  //设置小时为0
        c.set(Calendar.MINUTE, 59); //设置分钟为0
        c.set(Calendar.SECOND, 59); //设置秒为0
        c.set(Calendar.MILLISECOND, 999);//设置毫秒为0
        return c.getTimeInMillis();
    }

    /**
     * 获取本周天最后时间
     * @return
     */
    public static long weektail() {
        Calendar c = Calendar.getInstance();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.set(Calendar.DAY_OF_WEEK,c.getFirstDayOfWeek()+6);
        c.set(Calendar.HOUR_OF_DAY,23);
        c.set(Calendar.MINUTE,59);
        c.set(Calendar.SECOND,59);
        return c.getTimeInMillis();
    }
    /**
     * 获取本月最后一天的lang
     * @return lang
     */
    public static long monthtail(){
        Calendar c = Calendar.getInstance();
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));//设置当前月份为本月的最后一天
        c.set(Calendar.HOUR_OF_DAY, 23);//将小时至23
        c.set(Calendar.MINUTE, 59);//将分钟至59
        c.set(Calendar.SECOND,59);//将秒至59
        c.set(Calendar.MILLISECOND, 999);//将毫秒至999
        return c.getTimeInMillis();
    }

    public static long three(){
        Calendar c = Calendar.getInstance();
        int i = c.get(Calendar.DAY_OF_MONTH);
        c.set(Calendar.DAY_OF_MONTH, i-3);//设置当前月份为本月的最后一天
        c.set(Calendar.HOUR_OF_DAY, 23);//将小时至23
        c.set(Calendar.MINUTE, 59);//将分钟至59
        c.set(Calendar.SECOND,59);//将秒至59
        c.set(Calendar.MILLISECOND, 999);//将毫秒至999
        return c.getTimeInMillis();
    }


}
